package com.phuc.game.tankshooter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.TextView;

import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.phuc.game.tankshooter.common.Utility;
import com.phuc.game.tankshooter.moregame.Utils;

public class MenuActivity extends Activity {
	
	public static int maxScore;
	TextView txtScore;
	private AdView adView;
	
	public static int screenWidth;
	public static int screenHeight;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Display display = getWindowManager().getDefaultDisplay(); 
		screenWidth = display.getWidth();  // deprecated
		screenHeight = display.getHeight();  // deprecated
		MainActivity.SCENE_WIDTH = MainActivity.SCENE_HEIGH * screenWidth / screenHeight;
		
		setContentView(R.layout.menu_tank_game);
		txtScore = (TextView) findViewById(R.id.menu_txtPoint);
        Typeface font = Typeface.createFromAsset(
        this.getAssets(), 
        "fonts/UVNVan.TTF");
        txtScore.setTypeface(font);
		maxScore = Utility.getPreferenceInt("MAX_SCORE", 0, this);
		txtScore.setText("Kỉ luc: " + maxScore);
		
		// Load Ads
	    adView = (AdView) findViewById(R.id.adView);
	    new Thread() {
        	public void run() {
        		adView.loadAd(new AdRequest());
        	}
        }.start();
        
        initImageLoader(this);
	}
	
	public static void initImageLoader(Context context) {
		// This configuration tuning is custom. You can tune every option, you may tune some of them,
		// or you can create default configuration by
		//  ImageLoaderConfiguration.createDefault(this);
		// method.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.writeDebugLogs() // Remove for release app
				.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		Utility.savePreferenceInt("MAX_SCORE", maxScore, this);
		txtScore.setText("Kỉ lục: " + maxScore);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if (requestCode == 2000) {
			finish();
		}
	}
	
	@Override
	public void onBackPressed() {
		Intent intent = new Intent(this, MoreGameActivity.class);
		startActivityForResult(intent, 2000);
	}
	
	public void onStartClick(View v) {
		int tutorial = Utility.getPreferenceInt("TUTORIAL", 0, this);
		if (tutorial == 0) {
			Utility.savePreferenceInt("TUTORIAL", 1, this);
			Intent intent  = new Intent(this, TutorialActivity.class);
			startActivity(intent);
		} else {
			Intent intent = new Intent(this, MainActivity.class);
			startActivity(intent);
		}
	}
	
	public void onAboutClick(View v) {
		Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(
				"https://play.google.com/store/apps/details?id=bigkool.gamepro&amp;referrer=cpid=65"));
		startActivity(i);
	}

	public void onExitClick(View v) {
		Intent intent = new Intent(this, MoreGameActivity.class);
		startActivityForResult(intent, 2000);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (adView != null) {
			adView.destroy();
		}
		Utility.unbindDrawable(findViewById(R.id.root));
	}
	
}
